//
//  CJDownloadViewController.m
//  CoJellyCartoon
//
//  Created by kingbw on 16/4/12.
//  Copyright © 2016年 CJG. All rights reserved.
//

#import "CJDownloadViewController.h"
#import <SystemServices.h>
#import "NSString+Tools.h"
#import "CJDownloadTableViewCell.h"
#import "DownloadPlayViewController.h"
#define SystemSharedServices [SystemServices sharedServices]
@interface CJDownloadViewController ()<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, strong) UITableView *table;
@property (nonatomic, strong) NSMutableArray *downloadArray;
@property (nonatomic, strong) UILabel *containLabel;
@property (nonatomic, strong) UIView *pressView;
@property (nonatomic, strong) UIView *bacView;
@end

@implementation CJDownloadViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self creatContainLabel];
    [self creatTableView];
    [self getData];
   
}

- (void)creatTableView {
    self.table = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight - 84) style:UITableViewStylePlain];
    
    self.table.delegate = self;
    self.table.dataSource = self;
    [self.table setSeparatorColor:UITableViewCellSeparatorStyleNone];
    [self.view addSubview:self.table];
    [self.table registerClass:[CJDownloadTableViewCell class] forCellReuseIdentifier:@"CJDownloadTableViewCellIdentifier"];
    
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.downloadArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    CJDownloadTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CJDownloadTableViewCellIdentifier"];
    cell.nameLabel.text = self.downloadArray[indexPath.row];
    cell.numberLabel.text = [NSString stringWithFormat:@"%ld.", indexPath.row + 1];
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return ScreenHeight / 12;
}
- (void)creatContainLabel {
    self.bacView = [[UIView alloc] initWithFrame:CGRectMake(0, ScreenHeight - 84, ScreenWidth, 25)];
    self.bacView.backgroundColor = [UIColor colorWithRed:230 / 255.0 green:230 / 255.0 blue:230 / 255.0 alpha:1];
    [self.view addSubview:self.bacView];
    
    self.containLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, 25)];
    self.containLabel.text = [NSString stringWithFormat:@"主存储:%@ / 可用:%@", [SystemSharedServices diskSpace], [SystemSharedServices freeDiskSpaceinRaw]];
    self.containLabel.textColor = [UIColor blackColor];
    self.containLabel.font = [UIFont systemFontOfSize:14];
    
    self.pressView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width * (1 - [[[SystemSharedServices freeDiskSpaceinRaw] deleStringLastChar] floatValue] / [[[SystemSharedServices diskSpace] deleStringLastChar] floatValue]), 25)];
    
    self.pressView.backgroundColor = [UIColor colorWithRed:240 / 255.0 green:102 / 255.0 blue:144 / 255.0 alpha:1];
    
    [self.pressView addSubview:self.containLabel];
    [self.bacView addSubview:self.pressView];

}

- (void)getData {
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    //(@"=,=%@", paths);
    NSString *docDirectory = [paths objectAtIndex:0];
    
    NSFileManager *fm = [NSFileManager defaultManager];
    
    NSArray *files = [fm subpathsAtPath:docDirectory];
    
    self.downloadArray = [NSMutableArray arrayWithCapacity:0];
    
    for (NSString *str in files) {
        if ([str hasSuffix:@"mp4"]) {
            [self.downloadArray addObject:str];
        }
    }
}

#pragma mark - 删除
//重写系统提供按钮的方法
- (void)setEditing:(BOOL)editing animated:(BOOL)animated {
    //首先让父类调用
    [super setEditing:editing animated:animated];
    //让tableView处于编辑状态下
    [self.table setEditing:YES animated:YES];
}

//设置删除样式

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
    return UITableViewCellEditingStyleDelete;
}

//是否允许编辑
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    return YES;
}

//完成编辑
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        
        
        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        
        NSString *docDirectory = [paths objectAtIndex:0];
        
        NSFileManager *fm = [NSFileManager defaultManager];
        
        
        NSString *str = self.downloadArray[indexPath.row];
        NSString *fullPath = [NSString stringWithFormat:@"%@/%@", docDirectory, str];
        
        [self.downloadArray removeObjectAtIndex:indexPath.row];
        
        [self.table deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationRight];
        
        [fm removeItemAtPath:fullPath error:nil];
        
       
        
        [self.table reloadData];
    }
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    DownloadPlayViewController *downloadPlay = [[DownloadPlayViewController alloc] init];
    downloadPlay.string = self.downloadArray[indexPath.row];
    [self presentViewController:downloadPlay animated:YES completion:^{
        
    }];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
